bash
This demonstrates redirecting both standard output and standard error to a single log file in Bash.
python hello.py > "output-and-error.log" 2>&1 # redirect both output and errors to output-and-error.log # &1 means file descriptor 1 (stdout), so 2>&1 redirects stderr (2) to the current # destination of stdout (1), which has been redirected to output-and-error.log.
bash internalfile and stream operationsstream redirection and pipingcombined redirection
bash
This demonstrates various methods of redirecting stdout
and stderr
in Bash, including appending, discarding, and combining streams.
python hello.py > output.txt # stdout to (file) python hello.py >> output.txt # stdout to (file), append python hello.py 2> error.log # stderr to (file) python hello.py 2>&1 # stderr to stdout python hello.py 2>/dev/null # stderr to (null) python hello.py >output.txt 2>&1 # stdout and stderr to (file), equivalent to &> python hello.py &>/dev/null # stdout and stderr to (null) echo "$0: warning: too many users" >&2 # print diagnostic message to stderr
bash internalfile and stream operationsstream redirection and pipingstdout redirection
bash
This script redirects all output and errors from the python hello.py
command to /dev/null
, effectively suppressing any output or errors.
python hello.py > /dev/null 2>&1 # redirect all output and errors to the black hole, /dev/null, i.e., no output
bash internalfile and stream operationsstream redirection and pipingoutput suppression
bash
This demonstrates multiple methods to overwrite a file with a string in Bash, including process substitution, direct redirection, piping to cat
, and piping to tee
.
cat > output.out <(echo "#helloworld") echo "#helloworld" > output.out echo "#helloworld" | cat > output.out echo "#helloworld" | tee output.out >/dev/null
bash internalfile and stream operationsstream redirection and pipingfile output